package com.yjhb.android.sbz.activity;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.TaskListItemAdapter;
import com.yjhb.android.sbz.entity.TaskInfo;
import com.yjhb.android.sbz.imps.BaseTextChangedListener;
import com.yjhb.android.sbz.mvp.search.SearchPresenter;
import com.yjhb.android.sbz.mvp.search.SearchView;
import com.yjhb.android.sbz.tool.Tool;
import com.yjhb.android.sbz.view.RefreshHead;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/24 16:15
 */
public class SearchActivity extends BaseActivity<SearchPresenter> implements SearchView {
    private final String TAG = "SearchActivity";
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.search_tv)
    TextView search_tv;
    @BindView(R.id.refreshHeader)
    RefreshHead refreshHeader;
    @BindView(R.id.recyclerView)
    EmptyRecyclerView recyclerView;
    @BindView(R.id.content_layout)
    LinearLayout contentLayout;
    @BindView(R.id.footer)
    BallPulseFooter footer;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.clean_iv)
    ImageView clean_iv;

    final int size = 25;
    int current = 1;

    List<TaskInfo> tasks = new ArrayList<>();

    TaskListItemAdapter adapter;

    @Override
    protected SearchPresenter initPresenter() {
        return new SearchPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }


    @Override
    protected void initUI() {
        setStatusBarLightMode(true);
        recyclerView.setEmptyViewById(R.id.layout_empty);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter = new TaskListItemAdapter(this));
        adapter.setData(tasks);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                current++;
                search();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                current = 1;
                search();
            }
        });
        searchEt.addTextChangedListener(new BaseTextChangedListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s == null || s.length() < 1) {
                    clean_iv.setVisibility(View.INVISIBLE);
                } else {
                    clean_iv.setVisibility(View.VISIBLE);
                }
            }
        });
        searchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    current = 1;
                    search();
                    return true;
                }
                return false;
            }
        });

        adapter.setOnItemClick(new BaseRecyclerAdapter.ItemClickListener<TaskInfo>() {
            @Override
            public void onItemClick(int index, TaskInfo task) {
                Tool.startEditTaskDetailActivity(getActivity(), task.getId());
            }

            @Override
            public void onViewClick(View view, String msg, int index, TaskInfo task) {

            }
        });
    }

    private void search() {
        String key = Tool.getText(searchEt, "").trim();
        presenter.search(key, size, current);
    }

    @Override
    public void searchWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void searchSuccess(List<TaskInfo> tasks) {
        smartRefreshLayout.finishRefresh();
        loadingDialog.cancel();
        this.tasks.clear();
        this.tasks.addAll(tasks);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void searchLoadSuccess(List<TaskInfo> tasks) {
        smartRefreshLayout.finishLoadmore();
        loadingDialog.cancel();
        this.tasks.addAll(tasks);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        smartRefreshLayout.finishLoadmore();
        smartRefreshLayout.finishRefresh();
        ZxToast.showINFOBottomToast(msg);
    }

    @OnClick(R.id.cancel_iv)
    void cancelTv() {
        finish();
    }

    @OnClick(R.id.search_tv)
    void search_tv() {
        current = 1;
        search();
    }

    @OnClick(R.id.clean_iv)
    void clean_iv() {
        searchEt.setText("");
    }


}
